<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="用户名" prop="userName">
        <el-input v-model="dataForm.userName" placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item label="登陆密码" prop="password">
        <el-input v-model="dataForm.password" placeholder="登陆密码"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="phoneNumber">
        <el-input v-model="dataForm.phoneNumber" placeholder="手机号"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
      </el-form-item>
      <el-form-item label="积分" prop="points">
        <el-input v-model="dataForm.points" placeholder="积分"></el-input>
      </el-form-item>
      <el-form-item label="用户类型" prop="userType">
        <el-input v-model="dataForm.userType" placeholder="用户类型"></el-input>
      </el-form-item>
      <el-form-item label="头像" prop="userIcon">
        <!-- <el-input v-model="dataForm.userIcon" placeholder="头像"></el-input> -->
        <single-upload v-model="dataForm.userIcon"></single-upload>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import SingleUpload from "@/components/upload/singleUpload";
export default {
  components: { SingleUpload },
  data() {
    return {
      visible: false,
      dataForm: {
        userId: 0,
        userName: "",
        password: "",
        phoneNumber: "",
        email: "",
        points: "",
        userType: "",
        userIcon: "",
      },
      dataRule: {
        userName: [
          { required: true, message: "用户名不能为空", trigger: "blur" },
        ],
        password: [
          { required: true, message: "登陆密码不能为空", trigger: "blur" },
        ],
        phoneNumber: [
          { required: true, message: "手机号不能为空", trigger: "blur" },
        ],
        email: [{ required: true, message: "邮箱不能为空", trigger: "blur" }],
        points: [{ required: true, message: "积分不能为空", trigger: "blur" }],
        userType: [
          { required: true, message: "用户类型不能为空", trigger: "blur" },
        ],
        userIcon: [
          { required: true, message: "头像不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    init(id) {
      this.dataForm.userId = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.userId) {
          this.$http({
            url: this.$http.adornUrl(`/user/user/info/${this.dataForm.userId}`),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.userName = data.user.userName;
              this.dataForm.password = data.user.password;
              this.dataForm.phoneNumber = data.user.phoneNumber;
              this.dataForm.email = data.user.email;
              this.dataForm.points = data.user.points;
              this.dataForm.userType = data.user.userType;
              this.dataForm.userIcon = data.user.userIcon;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/user/user/${!this.dataForm.userId ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              userId: this.dataForm.userId || undefined,
              userName: this.dataForm.userName,
              password: this.dataForm.password,
              phoneNumber: this.dataForm.phoneNumber,
              email: this.dataForm.email,
              points: this.dataForm.points,
              userType: this.dataForm.userType,
              userIcon: this.dataForm.userIcon,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
